import 'package:flutter/material.dart';
import 'package:card_swiper/card_swiper.dart';

class SwipePage extends StatefulWidget {
  const SwipePage({Key? key}) : super(key: key);

  @override
  State<SwipePage> createState() => _SwipePageState();
}

class _SwipePageState extends State<SwipePage> {
  final List<String> _guideList = [
    'https://shirne.oss-cn-shenzhen.aliyuncs.com/website-mapp/wall/wall-5.jpg',
    'https://shirne.oss-cn-shenzhen.aliyuncs.com/website-mapp/wall/wall-3.jpg',
    'https://shirne.oss-cn-shenzhen.aliyuncs.com/website-mapp/wall/wall-7.jpg',
    'https://shirne.oss-cn-shenzhen.aliyuncs.com/website-mapp/wall/wall-2.jpg',
  ];
  final List<String> _guideInfoList = [
    "岁月悠长",
    "山河无恙",
    "君如皎月",
    "我如微尘",
  ];

  bool isLast = false;

  _gotoHome(BuildContext context) {
    Navigator.pop(context);
  }

  @override
  Widget build(BuildContext context) {
    final screenHeight = MediaQuery.of(context).size.height;
    return Stack(
      children: [
        Swiper(
          itemBuilder: (BuildContext context, int index) {
            return _SceneWidget(_guideList[index], _guideInfoList[index]);
          },
          itemCount: _guideList.length,
          pagination: const SwiperPagination(),
          loop: false,
          onIndexChanged: (index) {
            if (index == _guideList.length - 1) {
              setState(() {
                isLast = true;
              });
            } else {
              setState(() {
                isLast = false;
              });
            }
          },
          //control: new SwiperControl(),
        ),
        AnimatedPositioned(
          duration: const Duration(milliseconds: 500),
          curve: Curves.easeInOut,
          bottom: isLast ? -100 : screenHeight * 0.2,
          left: 0,
          right: 0,
          child: const Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text(
                '向右滑动',
                style: TextStyle(
                  fontSize: 12,
                  color: Color.fromRGBO(255, 255, 255, 0.4),
                  decoration: TextDecoration.none,
                ),
              ),
              Icon(
                Icons.arrow_right,
                color: Color.fromRGBO(255, 255, 255, 0.4),
              ),
            ],
          ),
        ),
        AnimatedPositioned(
          duration: const Duration(milliseconds: 500),
          curve: Curves.easeInOut,
          bottom: isLast ? screenHeight * 0.2 : -100,
          left: 0,
          right: 0,
          child: Center(
            child: ElevatedButton(
              style: ButtonStyle(
                padding: MaterialStateProperty.all(
                  const EdgeInsets.symmetric(
                    vertical: 10,
                    horizontal: 20,
                  ),
                ),
              ),
              onPressed: () {
                _gotoHome(context);
              },
              child: const Text('Go !'),
            ),
          ),
        ),
      ],
    );
  }
}

class _SceneWidget extends StatelessWidget {
  const _SceneWidget(this._image, this._title) : super();

  final String _image;
  final String _title;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      extendBodyBehindAppBar: true,
      body: Stack(
        children: <Widget>[
          Image.network(
            _image,
            fit: BoxFit.cover,
            width: double.infinity,
            height: double.infinity,
          ),
          Center(
            child: Text(
              _title,
              style: const TextStyle(
                fontSize: 20,
                color: Color.fromRGBO(255, 255, 255, 0.6),
                decoration: TextDecoration.none,
              ),
            ),
          ),
        ],
      ),
    );
  }
}
